<?php
/**
 *
 * ds
 * 2020.08.13
 */
namespace App\Models\MainDB;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class MallOrders extends Model{

    protected $table = 'ln_mall_orders';     //数据库内完整表名

    protected $primaryKey = 'id';    //主键名

    protected $fillable = [
        'id',      // 主键id
        'user_id_buyer',         //  购买人id
        'user_id_owner',         //  拥有人id
        'product_id',         //  产品id
        'price_id',                     //  产品价格id
        'expired_time',         //  过期时间
        'renew_count',         //  续费次数
        'price',         //  购买价格
        'day',         //  购买天数
        'state',         //  0未使用1使用中2已过期
        'add_time',         //  购买时间
        'use_time',         //  使用时间
        'type',         //  1自己购买2朋友赠送3周星榜奖励4活动兑换
        'category_id',         //  类别id
        'coin',         //
    ];

    public $timestamps = false;

    public   function  mall_products(){
        return $this->hasOne('App\Models\MainDB\MallProducts','id','product_id');
    }

    public   function  mall_categories(){
        return $this->hasOne('App\Models\MainDB\MallCategories','id','category_id');
    }
    //购买人id
    public   function  user_buyer(){
        return $this->hasOne('App\Models\MainDB\UserData','user_id','user_id_buyer');
    }
    //拥有者id
    public  function  user_owner(){
        return $this->hasOne('App\Models\MainDB\UserData','user_id','user_id_owner');
    }
    public function give($product_id, $user_id, $use_days = 0)
    {
        if ($product_id <= 0) {
            return false;
        }
        if ($user_id <= 0) {
            return false;
        }

        $user = app('model_user')->where('user_id', $user_id)->where('deleted', 0)->first();
        if (!$user) {
            return false;
        }
        $product = app('model_mall_products')->where('id', $product_id)->first();
        if (!$product) {
            return false;
        }

        $now_time = Carbon::now()->timestamp;
        $detail = $this->where(['product_id' => $product_id, 'user_id_owner' => $user_id])->first();

        if ($product->category_id == 5) {
            if ($detail) {
                $this->where('id', $detail->id)->increment('number');
            } else {
                $this->insert(
                    [
                        'user_id_buyer' => 0,
                        'user_id_owner' => $user_id,
                        'product_id'    => $product_id,
                        'price_id'      => 0,
                        'number'        => $use_days,
                        'state'         => 0,
                        'add_time'      => $now_time,
                        'price'         => 0,
                        'day'           => 0,
                        'type'          => 3,
                        'expired_time'  => 0,
                        'category_id'   => $product->category_id,
                        'coin'          => 0
                    ]
                );
            }
            return true;
        }

        if ($detail) {
            if ($detail->state == 2 || $detail->expired_time < $now_time) {
                $update_data = [
                    'state'        => 0,
                    'expired_time' => $now_time + $use_days * 86400
                ];
            } else {
                $update_data = [
                    'expired_time' => $detail->expired_time + $use_days * 86400
                ];
            }
            $re = $this->where('id', $detail->id)->update($update_data);
        } else {
            $expired_time = -1;
            if ($use_days > 0) {
                $expired_time = $now_time + $use_days * 86400;
            }
            $re = $this->insert(
                [
                    'user_id_buyer' => 0,
                    'user_id_owner' => $user_id,
                    'product_id'    => $product_id,
                    'price_id'      => 0,
                    'state'         => 0,
                    'add_time'      => $now_time,
                    'price'         => 0,
                    'day'           => 0,
                    'type'          => 3,
                    'expired_time'  => $expired_time,
                    'category_id'   => $product->category_id,
                    'coin'          => 0
                ]
            );
        }
        if (!$re) {
            return false;
        }
        return true;
    }


}
